package main

import (
	"fmt"
	"math"
	"perceptron/correlation"
	parsedata "perceptron/parseData"
	"perceptron/perceptron/regressor"

	"gonum.org/v1/gonum/mat"
)

func main() {

	matrix, names, err := parsedata.ParseCSV("./../Laptop_price.csv")

	if err != nil {
		panic(err)
	}

	for {
		corr := correlation.CorrMatrix(matrix)
		correlation.PrintCorrMatrix(corr, names)
		correlation.CreateHeatMap(corr)

		fmt.Println("Хотите удалить параметр? (y/n)")

		var resp rune
		fmt.Scanf("%c", &resp)
		if resp != 'y' {
			break
		}

		fmt.Println("Введите номер столбца:")
		var column int
		fmt.Scan(&column)
		column--

		rows, cols := matrix.RawMatrix().Rows, matrix.RawMatrix().Cols

		tmp := make([]float64, 0)

		for i := 0; i < rows; i++ {
			for j := 0; j < cols; j++ {
				if j == column {
					continue
				}
				tmp = append(tmp, matrix.At(i, j))
			}
		}

		matrix = mat.NewDense(rows, cols-1, tmp)
		names = append(names[:column], names[column+1:]...)
	}

	X, y := denseToData(matrix)

	lLen := int(float64(len(y)) * 0.8)

	lX, ly, testX, testy := X[:lLen], y[:lLen], X[lLen:], y[lLen:]

	perc := &regressor.Regressor{}
	err = perc.Init([]uint{10, 10}, 80000, len(X[0]), 1e-9)
	if err != nil {
		panic(err)
	}
	perc.Fit(lX, ly)
	s, v := perc.Test(testX, testy)

	fmt.Println(s, int(v), ". Средняя ошибка: ", math.Sqrt(v/float64(len(testy))))
	fmt.Println(perc.Run(testX[0]), testy[0])
	fmt.Println(perc.Run(lX[0]), ly[0])

}

func denseToData(m *mat.Dense) ([][]float64, []float64) {
	X := make([][]float64, m.RawMatrix().Rows)
	y := make([]float64, 0)

	for i := 0; i < m.RawMatrix().Rows; i++ {
		X[i] = make([]float64, m.RawMatrix().Cols-1)
		for j := 0; j < m.RawMatrix().Cols-1; j++ {
			X[i][j] = m.At(i, j)
		}
		y = append(y, m.At(i, m.RawMatrix().Cols-1))
	}
	return X, y
}
